/*
                  InterfaceIngame Class Implementation
*/
#include "InterfaceIngame.h"

InterfaceIngame::InterfaceIngame()
{
    cbxMessages = NULL;
    frmMessages = NULL;
    frmScoreboard = NULL;
    lblName = NULL;
    lblScore = NULL;
    lblCurrentWinner = NULL;
}

InterfaceIngame::~InterfaceIngame()
{
    SnakeEngine::Quit();
    Scores.clear();
}

int InterfaceIngame::Initialise()
{
    frmScoreboard = new Frame;
    frmScoreboard->Initialise(Point(50, 50), 1, 1);
    frmScoreboard->setBackground("data//interface//ingame//scoreboard.png");
    frmScoreboard->setVisible(true);
    frmScoreboard->setFixed(false);
    frmScoreboard->WindowAlign(ALIGN_RIGHT, GetWindowSize());
    frmScoreboard->WindowAlign(ALIGN_TOP, GetWindowSize());
    frmScoreboard->MoveRelative(-15, 15);

    lblName = new Label;
    lblName->Initialise(Point(15, 15), "data//fonts//gui.ttf", 26, "Name", GetSDLColor(255, 255, 255));

    lblScore = new Label;
    lblScore->Initialise(Point(150, 15), "data//fonts//gui.ttf", 26, "Score / " + lexical_cast<string>(SnakeEngine::getScoreToWin()), GetSDLColor(255, 255, 255));

    lblCurrentWinner = new Label;
    lblCurrentWinner->Initialise(Point(15, 250), "data//fonts//gui.ttf", 24, "Current Winner: N/A", GetSDLColor(255, 255, 255));

    vector<string> PlayerInfo = SnakeEngine::getScoreboardInfo();

    for (int i = 0; i < PlayerInfo.size(); i++)
    {
        Label *temp = new Label;

        vector<string> Info = SplitString(PlayerInfo.at(i), "\t");

        if (Info.size() < 2)
            continue;

        temp->Initialise(Point(15, 50 + (30 * i)), "data//fonts//gui.ttf", 24, Info.at(0), GetSDLColor(255, 255, 255));

        frmScoreboard->AddWidget(temp, "lbl" + lexical_cast<string>(i) + "name");

        temp = new Label;
        temp->Initialise(Point(150, 50 + (30 * i)), "data//fonts//gui.ttf", 24, Info.at(1), GetSDLColor(255, 255, 255));

        Scores.push_back(temp);

        frmScoreboard->AddWidget(temp, "lbl" + lexical_cast<string>(i) + "score");
    }

    frmScoreboard->AddWidget(lblName, "lblName");
    frmScoreboard->AddWidget(lblScore, "lblScore");
    frmScoreboard->AddWidget(lblCurrentWinner, "lblCurrentWinner");

    frmMessages = new Frame;
    frmMessages->Initialise(Point(25, 25), 300, 100);
    frmMessages->setVisible(true);
    frmMessages->setFixed(false);

    cbxMessages = new Chatbox;
    cbxMessages->Initialise(Point(0, 0), "data//fonts//bold.ttf", 14, 250, 100, 50);
    cbxMessages->setAutoscroll(true);

    frmMessages->AddWidget(cbxMessages, "cbxMessages");

    frmgHUD.AddFrame(frmScoreboard, "frmScoreboard");
    frmgHUD.AddFrame(frmMessages, "frmMessages");

    imgBackground.Initialise(ConfigFile::strBGPath, Point(0, 0));

    return 0;
}

InterfaceType InterfaceIngame::HandleInput(const SDL_Event& event)
{
    InterfaceType EngineReturn = NONE;

    EngineReturn = SnakeEngine::HandleInput(event);

    if (EngineReturn != NONE)
    {
        return EngineReturn;
    }

    frmgHUD.parseInput(event);

    return CheckWidgetEvents();
}

int InterfaceIngame::Draw(SDL_Surface *surface)
{
    imgBackground.BlitToSurface(surface);

    SnakeEngine::Update(cbxMessages);
    SnakeEngine::Draw(surface);

    if (SnakeEngine::getNeedScoreUpdate() == true)
    {
        UpdateScoreboard();
    }

    frmgHUD.BlitToSurface(surface);

    return 0;
}

void InterfaceIngame::UpdateScoreboard()
{
    vector<string> PlayerInfo = SnakeEngine::getScoreboardInfo();

    if (PlayerInfo.size() == 0)
    {
        return;
    }

    string strCurrentWinner = SplitString(PlayerInfo.at(0), "\t").at(0);
    int iCurrentWinnerScore = 0;
    int iScore = 0;

    for (int i = 0; i < PlayerInfo.size(); i++)
    {
        vector<string> Info = SplitString(PlayerInfo.at(i), "\t");

        Scores.at(i)->setText(Info.at(1));

        iScore = lexical_cast<int>(Info.at(1));

        if (iScore > iCurrentWinnerScore)
        {
            strCurrentWinner = Info.at(0);
            iCurrentWinnerScore = iScore;
        }
    }

    lblCurrentWinner->setText("Current Winner: " + strCurrentWinner);

    SnakeEngine::setNeedScoreUpdate(false);
}

InterfaceType InterfaceIngame::CheckWidgetEvents()
{
    return NONE;
}
